package com.luoc.base.wait;

/**
 *  证明wait会阻塞线程但会释放锁
 */
public class waitThread {

    public void testMethod(Object lock){
        synchronized (lock){
            System.out.println("waitThread 开始时间:"+ System.currentTimeMillis());
            try {
                lock.wait();
                System.out.println("waitThread 结束时间:"+ System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    class innerThread extends Thread {

        private Object lock;
        public innerThread(Object lock) {
            this.lock = lock;
        }

        @Override
        public void run() {

            waitThread waitThread = new waitThread();
            waitThread.testMethod(lock);
        }
    }

    public static void main(String[] args) {
         Object a = new Object();
         a = "b";

         Thread t1 = new waitThread().new innerThread(a);
         Thread t2 = new waitThread().new innerThread(a);
         t1.start();
         t2.start();

    }

}
